package com.fortune.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.DistributorOrderExcution;
import com.fortune.dto.in.distributorOrder.DistributorOrderAddDto;
import com.fortune.dto.in.distributorOrder.DistributorOrderEditDto;
import com.fortune.dto.in.distributorOrder.DistributorOrderListDto;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.DistributorOrderEnum;
import com.fortune.service.DistributorOrderMgtService;
import com.fortune.vo.distributorOrderMgt.DistributorOrder;
import com.fortune.vo.distributorOrderMgt.DistributorOrderDetail;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(value = "distributorOrderMgt", description = "经销商订单管理", tags = {"经销商订单管理"})
@RestController
@RequestMapping("distributorOrderMgt")
//@Validated
public class DistributorOrderMgtController {
	@Autowired
	private DistributorOrderMgtService distributorOrderMgtService;
	@ApiOperation(value = "经销商订单列表", nickname = "distributorOrderList", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParams({
		@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
		@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
		@ApiImplicitParam(name="search", value="搜索关键词",paramType="query",dataType="string"),
		@ApiImplicitParam(name="did", value="经销商id",paramType="query",dataType="string"),
		@ApiImplicitParam(name="type",value="是否批准",paramType="query",dataType="com.fortune.constants.enums.Approve",allowableValues = "NO,YES,ALL")
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<DistributorOrder>> list(HttpServletRequest request,@Validated @ApiIgnore DistributorOrderListDto distributorOrderMgtListDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		distributorOrderMgtListDto.setPage((distributorOrderMgtListDto.getPage()-1)*10);
		List<DistributorOrder> list = distributorOrderMgtService.list(mUserInfo,distributorOrderMgtListDto);
		int count = distributorOrderMgtService.count(mUserInfo, distributorOrderMgtListDto);
		PagingData<DistributorOrder> pagingData = new PagingData<>(list, count);
		return new DistributorOrderExcution<PagingData<DistributorOrder>>(pagingData, DistributorOrderEnum.SUCCESS);
	}
	
	@ApiOperation(value = "经销商订单详情", nickname = "distributorOrderDetail", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParams({
		@ApiImplicitParam(name="orderId", value="订单id",paramType="query",dataType="string",required=true),
		@ApiImplicitParam(name="dId", value="经销商id",paramType="query",dataType="string",required=true)
	})
    @GetMapping(value = "detail",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<DistributorOrderDetail> listDetails(HttpServletRequest request ,@Validated @NotBlank(message="订单id不能为空")String orderId,@Validated @NotBlank(message="经销商id不能为空")String dId) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String mId= mUserInfo.getmId();
		DistributorOrderDetail detail = distributorOrderMgtService.detail(mId, dId, orderId);
		if (detail==null) {
			return new DistributorOrderExcution<DistributorOrderDetail>(null, DistributorOrderEnum.FALSE);
		}
		return new DistributorOrderExcution<DistributorOrderDetail>(detail, DistributorOrderEnum.SUCCESS);
	}
	
	@ApiOperation(value = "添加经销商订单", nickname = "distributorOrderAdd", notes = "", tags={ "@岳希伟" })
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> list(HttpServletRequest request,@Validated @RequestBody DistributorOrderAddDto addDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		if(mUserInfo==null)
			return new DistributorOrderExcution<Void>(null, DistributorOrderEnum.NOLOIN);
		String mId= mUserInfo.getmId();
		String createUser =mUserInfo.getId();
		int result = distributorOrderMgtService.add(addDto, mId, createUser);
		if(result==0){
			return new DistributorOrderExcution<Void>(null, DistributorOrderEnum.FALSE);
		}else if (result==2) {
			return new DistributorOrderExcution<Void>(null, DistributorOrderEnum.NOPRODUCT);
		}
		return new DistributorOrderExcution<Void>(null, DistributorOrderEnum.SUCCESS);
	}
	
	@ApiOperation(value = "编辑经销商订单", nickname = "distributorOrderEdit", notes = "", tags={ "@岳希伟" })
    @PostMapping(value = "edit",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody DistributorOrderEditDto editDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		if(mUserInfo==null)
			return new DistributorOrderExcution<Void>(null, DistributorOrderEnum.NOLOIN);
		String mId= mUserInfo.getmId();
		String createUser =mUserInfo.getId();
		int result = distributorOrderMgtService.edit(editDto, mId, createUser);
		if(result==0)
			return new DistributorOrderExcution<Void>(null, DistributorOrderEnum.FALSE);
		return new DistributorOrderExcution<Void>(null, DistributorOrderEnum.SUCCESS);
	}
	
	@ApiOperation(value = "删除经销商订单", nickname = "distributorOrderDelete", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParam(name="orderId", value="订单id",paramType="query",dataType="string",required=true)
    @DeleteMapping(value = "delete",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="订单id不能为空")String orderId,@Validated @NotBlank(message="经销商id不能为空")String dId) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		if(mUserInfo==null)
			return new DistributorOrderExcution<Void>(null, DistributorOrderEnum.NOLOIN);
		String mId= mUserInfo.getmId();
		int result = distributorOrderMgtService.delete(mId, dId, orderId);
		if(result==0)
			return new DistributorOrderExcution<Void>(null, DistributorOrderEnum.FALSE);
		return new DistributorOrderExcution<Void>(null, DistributorOrderEnum.SUCCESS);
	}
	
	@ApiOperation(value = "批准发货", nickname = "distributorOrderAprove", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParams({
		@ApiImplicitParam(name="orderId", value="订单id",paramType="query",dataType="string",required=true),
		@ApiImplicitParam(name="dId", value="经销商id",paramType="query",dataType="string",required=true),
		@ApiImplicitParam(name="shipStore", value="分配仓库id",paramType="query",dataType="string",required=true)
	})
    @PutMapping(value = "aprove",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> aprove(HttpServletRequest request,@Validated @NotBlank(message="订单id不能为空")String orderId,@Validated @NotBlank(message="经销商id不能为空")String dId,@Validated @NotBlank(message="分配仓库id")String shipStore) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		if(mUserInfo==null)
			return new DistributorOrderExcution<Void>(null, DistributorOrderEnum.NOLOIN);
		String mId= mUserInfo.getmId();
		String createUser =mUserInfo.getId();
		int result = distributorOrderMgtService.approve(mId, dId, orderId,shipStore,createUser);
		if(result==0)
			return new DistributorOrderExcution<Void>(null, DistributorOrderEnum.FALSE);
		return new DistributorOrderExcution<Void>(null, DistributorOrderEnum.SUCCESS);
	}
}
